<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/20 0020
 * Time: 下午 3:43
 */

namespace App\Model\Backstage;

use App\Model\Base;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Staffattendance extends Base
{
    //员工考勤表
    protected $staff_attendance_table;
    protected $department_table;
    public $timestamps = false;
    public $SigninType = ['','看房','勘察','跑盘','派单','培训','上班','下班','迟到','早退','午餐','晚餐','加班','外出','请假','休假','陪看','其他'];
    public function __construct()
    {
        parent::__construct();
        $this->staff_attendance_table = 'staff_attendance_'.$this->branch_id;
//        $this->staff_attendance_table = 'staff_attendance';


        $this->department_table = 'department';
    }


    /** 上班打卡操作数据库
     * @param $dayTime
     * @return array
     */
    public function gotoWork($dayTime)
    {
        //今天上班有没有打卡
        $is_Work = DB::table($this->staff_attendance_table)
            ->where('employee_id', $this->uid)
            ->where('date', $dayTime)
            ->where('sign_type',6)
            ->value('start_work_time');

        //有没有外出
        $is_return = DB::table($this->staff_attendance_table)
            ->where('employee_id', $this->uid)
            ->where('date', $dayTime)
            ->where('sign_type','<>','6')
            ->select('knock_off_time','id')
            ->orderBy('id', 'desc')
            ->first();
       if($is_return) {
           if ($is_return->knock_off_time === null) {
               DB::table($this->staff_attendance_table)
                   ->where('id', $is_return->id)
                   ->update([
                       'knock_off_time' => time()
                   ]);
           }
       }
        if ($is_Work){
            return -1;
        }
        // 查询当前用户部门上班时间
        $workTime = DB::table($this->department_table)
            ->where('id', $this->store_id)
            ->value('to_work_time');

        $second = time()-(strtotime(date('Y-m-d ').$workTime));
        // 计算迟到时间
        if ($second > 0){
            // 迟到
            $res = DB::table($this->staff_attendance_table)
                ->insertGetId([
                    's_d_id'      => $this->store_id,
                    'date'        => $dayTime,
                    'employee_id' => $this->uid,
                    'start_work_time' => time(),
                    'hsa_status'  => 0,
                    'late_time'   => $second,
                    'sign_type'   =>6
                ]);
        }else{
            //正常上班
            $res = DB::table($this->staff_attendance_table)
                ->insertGetId([
                    's_d_id'      => $this->store_id,
                    'date'        => $dayTime,
                    'employee_id' => $this->uid,
                    'start_work_time' => time(),
                    'hsa_status'  => 1,
                     'sign_type'   =>6
                ]);
        }
        if (!$res){
            return false;
        }
        $data['userinfo'] = '【'.$this->name. '】您好！今天上班打卡成功';
        $data['worktime'] = '上班时间： '.$workTime;
        if ($second > 0) {
                $hour = floor($second/3600);
                $minute = floor(($second-3600 * $hour)/60);
                $result = $hour.'时'.$minute.'分';
                $data['info'] = '迟到'.$result;
        } else {
            $data['info']     = '打卡成功';
        }
        return $data;
    }

    /** 下班打卡操作数据库
     * @param $dayTime
     * @return array
     */
    public  function  afterWork($dayTime, $request, $province, $city, $IP)
    {
        $user = Auth::user();
        // 查询当前用户公司下班时间
        $workTime = DB::table($this->department_table)
            ->where('id', $this->store_id)
            ->value('after_work_time');

        //重复下班查询
        $data = DB::table($this->staff_attendance_table)
            ->where('employee_id', $user->u_id)
            ->where('date', $dayTime)
            ->where('sign_type',7)
            ->select('id')->first();

        //重复外出查询
        $goout = DB::table($this->staff_attendance_table)
            ->where('employee_id', $this->uid)
            ->where('sign_type','<>',6)
            ->where('sign_type','<>',7)
            ->where('date', $dayTime)
            ->select('knock_off_time','id')
            ->orderBy('id', 'desc')
            ->first();

        $second = time()-(strtotime(date('Y-m-d ').$workTime));

        //7type 下班
        if($request->input('sign_type') == 7) {
            if ($second >= 0) {
                //正常下班
                $hsa_status = 1;
             } else {
                $hsa_status = 0;
                //早退下班
             }
        }else{
            //外出
            if($goout && $goout->knock_off_time === null){
                $res = DB::table($this->staff_attendance_table)
                    ->where('id',$goout->id)
                    ->update([
                        's_d_id'      => $this->store_id,
                        'date'        => $dayTime,
                        'employee_id' => $this->uid,
                        'start_work_time' => time(),
                        'summary_work' =>$request->input('summary_work'),
                        'sign_type' =>$request->input('sign_type')
                    ]);
            }else{
                $res = DB::table($this->staff_attendance_table)
                    ->insertGetId([
                        's_d_id'      => $this->store_id,
                        'date'        => $dayTime,
                        'employee_id' => $this->uid,
                        'start_work_time' => time(),
                        'summary_work' =>$request->input('summary_work'),
                        'sign_type' =>$request->input('sign_type')
                    ]);
            }


        }

        if($request->input('sign_type') == 7) {
            if ($data) {
                $res = DB::table($this->staff_attendance_table)
                    ->where('id', $data->id)
                    ->update([
                        's_d_id' => $this->store_id,
                        'date' => $dayTime,
                        'employee_id' => $this->uid,
                        'start_work_time' => time(),
                        'hsa_status' => $hsa_status,
                        'summary_work' => $request->input('summary_work'),
                        'sign_type' => 7
                    ]);
            } else {
                $res = DB::table($this->staff_attendance_table)
                    ->insertGetId([
                        's_d_id' => $this->store_id,
                        'date' => $dayTime,
                        'employee_id' => $this->uid,
                        'start_work_time' => time(),
                        'hsa_status' => $hsa_status,
                        'summary_work' => $request->input('summary_work'),
                        'sign_type' => 7
                    ]);
            }
        }

        if (!$res) {
            return false;
        }

        $log = new Log();
        $log->addlog($this->store_id, $this->uid, 2, $IP.' 地址：'.$province.' '.$city);
        return true;
    }

    /** 根据时间查询打卡详情
     * @param $startTime
     * @param $endTime
     */
    public function staffRankings($map, $pageLimit)
    {

        $field = ['u_id', 'u_username', 'name','date','start_work_time','knock_off_time','hsa_status','summary_work','late_time','sign_type'];
        $list = DB::table($this->staff_attendance_table)
            ->leftjoin('user', 'employee_id', '=', 'u_id')
            ->leftJoin('department', 's_d_id', '=', 'department.id')
            ->where('user.is_del',0)
            ->where($map)
            ->select($field)
            ->orderBy($this->staff_attendance_table.'.id', 'desc')
            ->paginate($pageLimit);

        if (sizeof($list) == 0) {
            return false;
        }

        foreach($list as $k=>$v) {

            $v->hsa_status = $v->hsa_status === 0 ? '异常' : '正常';
            $v->sign_type = $this->SigninType[$v->sign_type];//考勤类型
            $v->start_work_time = date('H:i:s', $v->start_work_time);//上班时间
            if ($v->knock_off_time) {//下班时间
                $v->knock_off_time = date('H:i:s', $v->knock_off_time);
            } else {
                $v->knock_off_time = '';
            }
            if ($v->late_time) {//逾期时间
                $hour = floor(intval($v->late_time)/3600);
                $minute = floor((intval($v->late_time)-3600 * $hour)/60);
                $v->late_time = $hour.'时'.$minute.'分';
            } else {
                $v->late_time = '';
            }
            if(!$v->summary_work){
                $v->summary_work = '';
            }

        }
        return $list;
    }



}